package com.aeks.user;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.aeks.DB.OperateDB;

public class AboutUser {
	public static boolean check(String username, String password) {
		String sql = "select * from userinfo where username = '" + username
				+ "'";
		List<Map> list = OperateDB.executeQuery(sql);
		if (list.size() < 1) {// 数据库中没有该用户时，登陆失败，返回false
			return false;
		} else {
			Map map = list.get(0);
			String psw = (String) map.get("passkey");// 获得数据库中对应账号的密码
			if (!password.equals(psw)) {// 登陆时输入的密码与数据库中的不匹配,登陆失败，返回false
				return false;
			} else {// 登陆成功，返回true
				return true;
			}
		}
	}
	
	public static List<User> getUser(String username) {
		List<User> ulist = null;
		String sql = "select * from userinfo where username = '" + username
			+ "'";
		List<Map> list = new ArrayList<Map>(); 
		list = OperateDB.executeQuery(sql);
		if(list != null && list.size() > 0) {
			ulist = new ArrayList<User>();
			Map map = list.get(0);
			User user = new User();
			user.init(map);
			ulist.add(user);
		}
		return ulist;
	}
	
	public static boolean setParam(String limitValue, String titleValue, String briefValue, String keyValue, String textValue, String username) {
		boolean isSuccess = true;
		String sql = "update userinfo set limitvalue = " + limitValue  +
				", titlevalue = " + titleValue +
				", briefvalue = " + briefValue + 
				", keyvalue = " + keyValue + 
				", textvalue = " + textValue + 
				" where username = '" + username + "'";
		//System.out.println(sql);
		int n = OperateDB.update(sql);
		if (n <= 0) {
			isSuccess = false;
		}
		return isSuccess;
	}
	
	public static int setRandName(String saveName, String fileName, String userName) {
		String sql = "insert into loadfile values(null, ?, ?, ?)";
		List<Object> tlist = new ArrayList<Object>();
		tlist.add(userName);
		tlist.add(saveName);
		tlist.add(fileName);
		OperateDB.setList(tlist);
		int tn = OperateDB.getGeneratedKey(sql);

		return tn;
	}
}
